package com.example.lcpharmacy.controller;

import com.example.lcpharmacy.entity.GoodsInfoEntity;
import com.example.lcpharmacy.entity.resBean.ReturnGoodsInfo;
import com.example.lcpharmacy.entity.reqBean.GoodsInfoBean;
import com.example.lcpharmacy.lcutils.Pagination;
import com.example.lcpharmacy.lcutils.StringUtils;
import com.example.lcpharmacy.mapper.GoodsInfoDao;
import com.example.lcpharmacy.service.GoodsInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goodsInfo")
public class GoodsInfoImpl {
    @Autowired
    GoodsInfoDao goodsInfoDao;

    @Autowired
    GoodsInfoService goodsInfoService;

    @RequestMapping("getGoods")
    public List<GoodsInfoEntity> getGoods() {
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoDao.selectGoods();
        return goodsInfoEntities;
    }

    /**
     * 根据页面传来的当前页数、每页显示的条目数，查询并分页返回
     * pageNum : 代表当前页码
     */
//    @RequestMapping(value="getGoodsByPage", method = RequestMethod.POST)
    // ,@RequestBody String pageNum
    @RequestMapping("/getGoodsByPage")
    public PageInfo getGoodsByPage(@RequestBody Pagination pagination) {
        Integer currentPage = Integer.valueOf(pagination.getPageNum());
        if(pagination.getPageSize()!=0) {
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.goodsInfoPage(currentPage, pagination.getPageSize());
            return goodsInfoEntityPageInfo;
        }else {
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.goodsInfoPage(currentPage, 10);
            return goodsInfoEntityPageInfo;
        }
    }

    /**
     * 根据传回的商品编码或商品名称 查询
     *
     * @return
     */
    //I/O error while reading input message; nested exception is java.io.IOException: Stream closed
    //用了 两个@RequestBody 会报以上错。
    @RequestMapping("/queryByPost")
    public PageInfo getGoodsByPage(@RequestBody GoodsInfoBean goodsInfoBean) {

        //坑：在进行实体对象的非空判断时，如果客户端回传的参数中有该对象但是没有参数，在后台也会创建一个没有数据的对象
        Pagination page = goodsInfoBean.getPagination();
        GoodsInfoEntity goodsInfoEntity = goodsInfoBean.getGoodsInfo();
        //(StringUtils.isEmpty(goodsInfoEntity.getGoodsCode())) &&
        if (StringUtils.isEmpty(goodsInfoEntity.getGoodsCode()) && StringUtils.isEmpty(goodsInfoEntity.getGoodsName())) {
            //int pageNum = pagination.getPageNum();
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.goodsInfoPage(page.getPageNum(), 10);
            return goodsInfoEntityPageInfo;
        } else if (!StringUtils.isEmpty(goodsInfoEntity.getGoodsCode()) && !StringUtils.isEmpty(goodsInfoEntity.getGoodsName())) {
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.queryByCodeAName(goodsInfoEntity.getGoodsCode(), goodsInfoEntity.getGoodsName(), page.getPageNum(), 10);
            return goodsInfoEntityPageInfo;
        } else if (!StringUtils.isEmpty(goodsInfoEntity.getGoodsCode())) {
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.queryByGoodsCode(goodsInfoEntity.getGoodsCode(), page.getPageNum(), 10);
            return goodsInfoEntityPageInfo;
        } else if (!(StringUtils.isEmpty(goodsInfoEntity.getGoodsName()))) {
            PageInfo<GoodsInfoEntity> goodsInfoEntityPageInfo = goodsInfoService.queryByGoodsName(goodsInfoEntity.getGoodsName(), page.getPageNum(), 10);
            return goodsInfoEntityPageInfo;
        } else {
            return null;
        }
    }

    /**
     * 根据商品编码 删除商品信息
     */
    @RequestMapping("/deleteByCode")
    public Integer deleteByCode(@RequestBody GoodsInfoEntity goodsInfoEntity) {
        String goodsCode = goodsInfoEntity.getGoodsCode();
//        int pageNum = pagination.getPageNum();
        if (goodsInfoEntity.getGoodsCode() != null) {
            int i = goodsInfoDao.deleteByCode(goodsCode);
            if (i != 1) {
                return 0;
            }
        }
        return null;
    }

    @RequestMapping("/updateGoodsInfo")
    public String updateCategory(@RequestBody GoodsInfoEntity goodsInfoEntity) {
        System.out.println("updateGoodsInfo------"+goodsInfoEntity);
        String result  = goodsInfoService.updateGoodsInfo(goodsInfoEntity);
        return result;
    }

    /**
     * 查询单个商品 并拿到图片字段解析数据
     */
    @RequestMapping("/selectOne")
    public ReturnGoodsInfo selectOne(@RequestBody GoodsInfoEntity goodsInfoEntity) {
        ReturnGoodsInfo returnGoodsInfo = goodsInfoService.selectOne(goodsInfoEntity);
        return returnGoodsInfo;
    }
}
